package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/role")
@Tag(name = "角色管理模块")
// 统一跨域配置
//@CrossOrigin(allowCredentials = "true" , originPatterns = "*",
//        allowedHeaders="*" )
public class SysRoleController {
    @Resource
    SysRoleService sysRoleService;

    @Operation(summary = "查询所有角色并判断是否已分配")
    @GetMapping("all/{userId}")
    public Result<List<SysRole>> all(@PathVariable("userId")Long userId){
        List<SysRole> sysRoles = sysRoleService.listAll(userId);
        return Result.ok(sysRoles);
    }


    @Operation(summary = "根据id查询角色")
    @GetMapping("{id}")
    public Result<SysRole> getById(@PathVariable("id")Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }
    @Operation(summary = "更新角色")
    @PutMapping  //get方式没有请求体
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return Result.ok(null);
    }

    @Operation(summary = "新增角色")
    @PostMapping  //get方式没有请求体
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        //如果前端需要新增后的对象数据(新增后对象会有id、创建更新时间...) 也可以返回新增后的对象
        return Result.ok(null);
    }



    // delete + {id} 表示删除   get + {id} 根据id查询   put + 带id的对象  更新   post + 对象 表示新增
    @Operation(summary = "根据id删除角色")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id){
        sysRoleService.deleteById(id);
        return Result.ok(null).message("删除成功");
    }

    @Operation(summary = "条件查询分页列表")
    @Parameters(value = {
            @Parameter(description = "页码" , required = true , example = "1" , in = ParameterIn.PATH),
            @Parameter(description = "每页记录数" , required = true , example = "3" ,
                    in = ParameterIn.PATH),
            @Parameter(description = "搜索条件" , required = false , example = "系统" ,
                    in = ParameterIn.QUERY)
    })
    @GetMapping("page/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> page(@PathVariable("pageNum") Integer pageNum,
                                 @PathVariable("pageSize")Integer pageSize,
                                  String keyword){
        /*
            角色管理：操作的是sys_role表
            1、创建表映射的实体类SysRole
            2、创建SysRoleMapper接口
                定义带条件查询分页数据的方法
            3、创建SysRoleMapper.xml
                实现条件查询分页数据的方法
            4、创建业务类封装条件查询分页数据的代码返回数据
         */
        PageInfo<SysRole> pageInfo = sysRoleService.findPageByKeyword(pageNum, pageSize, keyword);
        return Result.build(ResultCodeEnum.SUCCESS , pageInfo);
    }


}
